package com.demos.webcommon.事务;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("TransactionService")
public class service {
    @Autowired
    private dao1 dao1;
    @Autowired
    private dao2 dao2;

    /**
     * 普通回滚（只有doAdd1方法的事务生效）
     * @param name
     * @return
     */
    @Transactional
    public int doAdd1(String name){
        int i = 0;
        i += privateAdd1(name);
        i += privateAdd2(name);
        return i;
    }

    /**
     * 自调方法回滚失败
     * @param name
     * @return
     */
    public int doAdd2(String name){
        int i = 0;
        i += privateAdd1(name);
        i += privateAdd2(name);
        return i;
    }


    @Transactional
    public int privateAdd1(String name){
        int i = dao1.add(name);
        return i;
    }
    @Transactional
    public int privateAdd2(String name){
        int i = dao2.add(name+"_ex");
        if(true) throw new RuntimeException("报错");
        return i;
    }
}
